package com.ajie.bluetoothcard.ui;



import java.util.ArrayList;


import com.ajie.bluetoothcard.adapter.CardTypeAdapter;
import com.ajie.bluetoothcard.db.DBApi;
import com.ajie.bluetoothcard.domain.CardType;
import com.ajie.bluetoothcard.pre.Preference;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;


/**
 * @author liujie
 *  @Date: 2013-12-04
 * @version 1.0.0
 */
public class MainCardBoxActivity extends BaseActivity implements OnItemClickListener{
	private ListView listView;
	private CardTypeAdapter cardTypeAdapter;
	private ArrayList<CardType> cardTypes = new ArrayList<CardType>();
	private Button add_card_type;
	private DBApi dbApi;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.card_box);
		showTop(R.string.card_list_type, true);
		dbApi = DBApi.getInstance(this);
		initData();
		initView();
	}
	private void initData() {
		if(Preference.getInstance().getIsFirst_insert_box()){
			Preference.getInstance().setFirst_insert_box(false);
			CardType cardType1 = new CardType("0", "全部", "");
			CardType cardType2 = new CardType("1", "未分组", "");
			CardType cardType3 = new CardType("2", "国企", "");
			CardType cardType4 = new CardType("3", "私企", "");
			CardType cardType5 = new CardType("4", "政府", "");
			CardType cardType6 = new CardType("5", "公安", "");
			cardTypes.add(cardType1);
			cardTypes.add(cardType2);
			cardTypes.add(cardType3);
			cardTypes.add(cardType4);
			cardTypes.add(cardType5);
			cardTypes.add(cardType6);
			dbApi.insertCardType(cardType1);
			dbApi.insertCardType(cardType2);
			dbApi.insertCardType(cardType3);
			dbApi.insertCardType(cardType4);
			dbApi.insertCardType(cardType5);
			dbApi.insertCardType(cardType6);
		}else{
			cardTypes = dbApi.getAllCardTypes();
		}
	}
	@Override
	protected void initView() {
		super.initView();
		LayoutInflater inflater = LayoutInflater.from(this);
		View header = inflater.inflate(R.layout.add_card_type, null);
		add_card_type = (Button) header.findViewById(R.id.add_card_type);
		add_card_type.setOnClickListener(this);
		listView = (ListView) findViewById(R.id.list);
		cardTypeAdapter = new CardTypeAdapter(this);
		listView.addFooterView(header);
		listView.setAdapter(cardTypeAdapter);
		listView.setOnItemClickListener(this);
		cardTypeAdapter.setData(cardTypes);
	}
	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.add_card_type:
			LayoutInflater factory = LayoutInflater.from(this) ;
			View myView = factory.inflate(R.layout.add_card_type_edit, null) ;
			final EditText editText = (EditText) myView.findViewById(R.id.add_card_type_edit);
			Dialog dialog = new AlertDialog.Builder(this)
				.setTitle("新的分类名")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(!"".equals(editText.getText().toString())){
							CardType cardType = new CardType(Preference.getInstance().getAdd_type_index()+"", editText.getText().toString(), "");
							dbApi.insertCardType(cardType);
							Preference.getInstance().setAdd_type_index(Preference.getInstance().getAdd_type_index()+1);
							cardTypes = dbApi.getAllCardTypes();
							cardTypeAdapter.setData(cardTypes);
						}
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				}).setView(myView).create();
			dialog.setCanceledOnTouchOutside(false);
			dialog.show() ;
		
			
			break;

		default:
			break;
		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
		startActivity(new Intent(this,CardListActivity.class).putExtra("cardType", cardTypes.get(position).getType()));
	}
}
